function ALOGchangeCheckbox() {
    localStorage.setItem ("po_alog_submit_logs", String(document.getElementById ('alog_checkbox').checked));
}

function ALOGinstallAutoLogger () {
    var ths = document.getElementsByTagName ("th");
    var tbl = null;
    for (var i=0; i<ths.length; i++) {
        if (ths[i].textContent == "Date") {
            tbl = ths[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
            break;
        }
    }
    if (!tbl) return;
    
    localStorage.setItem ("po_alog_submit_logs", "true");
    var div = document.createElement ("div");
    div.innerHTML = "<div style='color:lime;text-align:center;font-size:14px'><input id='alog_checkbox' type='checkbox' checked='true' onclick='ALOGchangeCheckbox();'> submit visited PvP combat logs automatically to the Federation logger.</input>\
                    </div>";
    tbl.parentNode.insertBefore (div, tbl);
    
    window.addEventListener('storage', ALOGstorageUpdated, false);
    ALOGshowProcessedEntries (localStorage.getItem ('po_alog_sent_combats'));
}

function ALOGstorageUpdated (evt) {
    if (evt.key != 'po_alog_sent_combats') return;
    ALOGshowProcessedEntries (evt.newValue);
}

function ALOGshowProcessedEntries (entries) {
    var ths = document.getElementsByTagName ("th");
    var tbl = null;

    for (var i=0; i<ths.length; i++) {
        if (ths[i].textContent == "Date") {
            tbl = ths[i].parentNode.parentNode.parentNode;
            break;
        }
    }
    if (!tbl || tbl.rows.length == 1) return;

    for (var i=1; i<tbl.rows.length; i++) {
        var row = tbl.rows[i];
        var cell = row.cells[2];
        if (cell.textContent == "Ship vs NPC") continue;
        var pid = cell.getAttribute("onclick").split ("(")[1].split(")")[0];
        if (entries.indexOf (pid) != -1) {
            cell.style.color = "lime";
            cell.setAttribute ("title", "Already submitted to Federation logger.")
        } else {
            cell.style.color = "orange";
            cell.setAttribute ("title", "Please open the log to submit it to the Federation logger.")
        }
    }
}

window.onload = ALOGinstallAutoLogger;